<?php
class ss_api_interface extends ss_konstrukt_masterComponent{
	public $mapper;
	public $data=array();
	
	public function dispatch(){
		//ss_error::log(SITE_ROOT_PATH.'/htdocs/api/interface.data');
		if(!file_exists(SITE_ROOT_PATH.'/htdocs/api/interface.data')) throw new k_PageNotFound();
		$this->data=unserialize(file_get_contents(SITE_ROOT_PATH.'/htdocs/api/interface.data'));
		return parent::dispatch();
	}
	
	public function data(){
		return $this->data;
	}
	
	protected function map($name){
		if(array_key_exists($name,$this->data)) return 'ss_api_inteface_detail';
	}
	
	protected function renderHtml(){
		ss_error::log('Render API HTML '.__CLASS__);
		$table='';
		foreach($this->data as $item){
			$row='<td><a href="'.$this->url($item['id']).'">open-></a><td>';
			foreach($item as $key=>$val){
				if(is_array($val) || is_object($val))$val=print_r($val,true);
				$row.="<td>{$val}</td>";
			}
			$table.='<tr>'.$row."</tr>\n";
		}
		if(is_array($item)) $table='<h1>'.$this->name()." master</h1><table>\n<tr><th></th><th>".implode('</th><th>',array_keys($item))."</th></tr>\n".$table."\n</table>\n";
		return k_HtmlResponse($table);
	}
	
	protected function renderJson(){
		ss_error::log('Render API JSON '.__CLASS__);
		/*$result=array();
		foreach($this->content as $row){
			$result[]=$row['name'];
		}*/
		return new k_JsonResponse($this->data);
	}
}

class ss_api_inteface_detail extends ss_konstrukt_detailComponent{
	protected $mapper;
	protected $data;
	protected $content;
	
	protected function renderHtml(){
		ss_error::log('Render API HTML '.__CLASS__);
		$table='';
		foreach($this->content as $key=>$val){
			if(is_array($val) || is_object($val))$val=print_r($val,true);
			$table.="<tr><td>{$key}</td><td>{$val}</td></tr>\n";
		}
		$table='<h1>'.$this->name()." detail</h1><table>\n".$table."\n</table>\n";
		return $table;
	}
	
	protected function renderJson(){
		ss_error::log('Render API JSON '.__CLASS__);
		//$name=$this->name();
		$data=$this->context->data();
		return new k_JsonResponse($data[$this->name()]);
	}
}
?>